package com.example.littleSpider.myapplication.fragment;

import android.Manifest;
import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.drawable.GradientDrawable;
import android.os.Build;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.annotation.RequiresApi;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.littleSpider.myapplication.MainActivity;
import com.example.littleSpider.myapplication.R;
import com.example.littleSpider.myapplication.activity.ActivityTwo;
import com.example.littleSpider.myapplication.activity.ChangeAddressActivity;
import com.example.littleSpider.myapplication.activity.CityListActivity;
import com.example.littleSpider.myapplication.activity.FindJobActivity;
import com.example.littleSpider.myapplication.activity.FindPartnerActivity;
import com.example.littleSpider.myapplication.activity.HurryUpActivity;
import com.example.littleSpider.myapplication.activity.NativePlaceActivity;
import com.example.littleSpider.myapplication.activity.WatchOperaActivity;
import com.example.littleSpider.myapplication.adapter.HomeRadapter;
import com.example.littleSpider.myapplication.base.BaseFragment;
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;
import com.littlespider.mylibrary.util.T;
import com.littlespider.mylibrary.view.WeChatCaptureActivity;
import com.zhouwei.mzbanner.MZBannerView;
import com.zhouwei.mzbanner.holder.MZHolderCreator;
import com.zhouwei.mzbanner.holder.MZViewHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import es.dmoral.toasty.Toasty;

/**
 * Created by aduser on 2017/12/5.
 */

public class HomePageFragment extends BaseFragment {

//    @BindView(R.id.text_view)
//    TextView mText;
    @BindView(R.id.banner_view)
    MZBannerView mzBannerView;
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    @BindView(R.id.refresh_view)
    TwinklingRefreshLayout refreshView;

    List<Integer> pictures = new ArrayList<>();
    @Override
    public int setLayout() {
        return R.layout.fragment_home_page;
    }

    @Override
    protected void initView() {
        pictures.add(R.drawable.spiderman_three);
        pictures.add(R.drawable.spiderman_two);
        pictures.add(R.drawable.spiderman_one);
        mzBannerView.setPages(pictures, new MZHolderCreator() {
            @Override
            public MZViewHolder createViewHolder() {
                return new MyPageHolder();
            }
        });
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        HomeRadapter homeRadapter = new HomeRadapter(new ArrayList(), getActivity());
        recyclerView.setAdapter(homeRadapter);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setFocusable(false);
        //如果缺少这句话, recyclerView将会滑动粘连
        recyclerView.setNestedScrollingEnabled(false);
        //设置上下拉监听事件
        refreshView.setOnRefreshListener(new RefreshAndLoad());
    }

    @Override
    protected void initData() {

    }

    /**
     * 轮播图下面的五个按钮的点击事件
     * @param view
     */
    @OnClick({R.id.rl_find_job, R.id.rl_find_partner, R.id.rl_native_place,
    R.id.rl_hurry_up, R.id.rl_watch_opera})
    public void fiveButtons(View view){
        Intent intent = new Intent();
        switch (view.getId()){
            case R.id.rl_find_job: //找工作
                intent.setClass(getActivity(), FindJobActivity.class);
                break;
            case R.id.rl_find_partner: //找对象
                intent.setClass(getActivity(), FindPartnerActivity.class);
//                intent.setClass(getActivity(), ActivityTwo.class);
//                intent.setClass(getActivity(), ChangeAddressActivity.class);
                break;
            case R.id.rl_native_place: //本地供求
                intent.setClass(getActivity(), NativePlaceActivity.class);
                break;
            case R.id.rl_hurry_up: //赶快
                intent.setClass(getActivity(), HurryUpActivity.class);
                break;
            case R.id.rl_watch_opera: //听戏看戏
                intent.setClass(getActivity(), WatchOperaActivity.class);
                break;
            default:
                break;
        }
        startActivity(intent);
    }

    class MyPageHolder implements MZViewHolder<Integer> {
        ImageView imageView;
        @Override
        public View createView(Context context) {
            View view = LayoutInflater.from(context).inflate(R.layout.view_banner, null);
            imageView = (ImageView) view.findViewById(R.id.image);
            return view;
        }

        @Override
        public void onBind(Context context, int i, Integer integer) {
            imageView.setImageResource(integer);
        }
    }

    /**
     * 上下拉监听适配器
     */
    class RefreshAndLoad extends RefreshListenerAdapter{

        @Override
        public void onRefresh(TwinklingRefreshLayout refreshLayout) {
            super.onRefresh(refreshLayout);
            T.show(getActivity(), "刷新");
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    refreshView.finishRefreshing();
                }
            }, 1000);
        }

        @Override
        public void onLoadMore(TwinklingRefreshLayout refreshLayout) {
            super.onLoadMore(refreshLayout);
            T.show(getActivity(), "加载");
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    refreshView.finishLoadmore();
                }
            }, 1000);
        }
    }
}
